package com.ruoyi.hotel.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hotel.domain.LyhmsOrder;
import com.ruoyi.hotel.domain.OrderPeopleInfo;
import com.ruoyi.hotel.mapper.LyhmsOrderMapper;
import com.ruoyi.hotel.mapper.LyhmsRoomMapper;
import com.ruoyi.hotel.mapper.OrderPeopleInfoMapper;
import com.ruoyi.hotel.service.ILyhmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * Service业务层处理
 * @author ruoyi
 * @date 2022-02-28
 */
@Service
public class LyhmsOrderServiceImpl implements ILyhmsOrderService {
    @Autowired
    private LyhmsOrderMapper lyhmsOrderMapper;
    @Resource
    private LyhmsRoomMapper lyhmsRoomMapper;

    @Resource
    private OrderPeopleInfoMapper orderPeopleInfoMapper;

    /**
     * 查询
     * @param id 主键
     * @return
     */
    @Override
    public LyhmsOrder selectLyhmsOrderById(Long id) {
        LyhmsOrder order = lyhmsOrderMapper.selectLyhmsOrderById(id);
        OrderPeopleInfo orderPeopleInfo = new OrderPeopleInfo();
        orderPeopleInfo.setOrderId(order.getId());
        List<OrderPeopleInfo> orderPeopleInfos = orderPeopleInfoMapper.selectOrderPeopleInfoList(orderPeopleInfo);
        if (!orderPeopleInfos.isEmpty()) {
            order.setPeopleInfos(orderPeopleInfos);
        }
        return order;
    }

    /**
     * 查询列表
     * @param lyhmsOrder
     * @return
     */
    @Override
    public List<LyhmsOrder> selectLyhmsOrderList(LyhmsOrder lyhmsOrder) {
        return lyhmsOrderMapper.selectLyhmsOrderList(lyhmsOrder);
    }

    /**
     * 新增
     * @param lyhmsOrder
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLyhmsOrder(LyhmsOrder lyhmsOrder) {

        lyhmsOrder.setCreateTime(DateUtils.getNowDate());
        int row = lyhmsOrderMapper.insertLyhmsOrder(lyhmsOrder);
        String roomName = lyhmsOrder.getRoomName();
        //修改房间状态
        lyhmsRoomMapper.updateByName(roomName, 2);
        List<OrderPeopleInfo> orderPeopleInfos = lyhmsOrder.getPeopleInfos();
        if (orderPeopleInfos != null && !orderPeopleInfos.isEmpty()) {
            for (OrderPeopleInfo orderPeopleInfo : orderPeopleInfos) {
                if (StringUtils.isNotEmpty(orderPeopleInfo.getIdNumber()) && StringUtils.isNotEmpty(orderPeopleInfo.getPeopleName())) {
                    orderPeopleInfo.setOrderId(lyhmsOrder.getId());
                    orderPeopleInfoMapper.insertOrderPeopleInfo(orderPeopleInfo);
                }
            }
        }
        return row;
    }

    /**
     * 修改
     * @param lyhmsOrder
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLyhmsOrder(LyhmsOrder lyhmsOrder) {
        int row = lyhmsOrderMapper.updateLyhmsOrder(lyhmsOrder);
        List<OrderPeopleInfo> orderPeopleInfos = lyhmsOrder.getPeopleInfos();
        if (!CollectionUtils.isEmpty(orderPeopleInfos)) {
            for (OrderPeopleInfo orderPeopleInfo : orderPeopleInfos) {
                if (orderPeopleInfo.getId() != null) {
                    orderPeopleInfoMapper.updateOrderPeopleInfo(orderPeopleInfo);
                } else {
                    if (StringUtils.isNotEmpty(orderPeopleInfo.getIdNumber()) && StringUtils.isNotEmpty(orderPeopleInfo.getPeopleName())) {
                        orderPeopleInfo.setOrderId(lyhmsOrder.getId());
                        orderPeopleInfoMapper.insertOrderPeopleInfo(orderPeopleInfo);
                    }
                }

            }
        }
        return row;
    }


    @Override
    public int updateOrder(LyhmsOrder lyhmsOrder) {
        int row = lyhmsOrderMapper.updateLyhmsOrder(lyhmsOrder);
        return row;
    }

    /**
     * 批量删除
     * @param ids 需要删除的主键
     * @return 结果
     */
    @Override
    public int deleteLyhmsOrderByIds(Long[] ids) {
        return lyhmsOrderMapper.deleteLyhmsOrderByIds(ids);
    }

    /**
     * 删除信息
     * @param id 主键
     * @return 结果
     */
    @Override
    public int deleteLyhmsOrderById(Long id) {
        return lyhmsOrderMapper.deleteLyhmsOrderById(id);
    }

    @Override
    @Transactional
    public int quxiao(Long id) {
        LyhmsOrder lyhmsOrder = selectLyhmsOrderById(id);
        int row = deleteLyhmsOrderById(id);
        String roomName = lyhmsOrder.getRoomName();
        lyhmsRoomMapper.updateByName(roomName, 1);
        return row;
    }

    @Override
    @Transactional
    public int tuifang(LyhmsOrder lyhmsOrder) {
        int row = updateOrder(lyhmsOrder);
        String roomName = lyhmsOrder.getRoomName();
        lyhmsRoomMapper.updateByName(roomName, 4);
        return row;
    }

    @Override
    @Transactional
    public int ruzhu(LyhmsOrder lyhmsOrder) {
        int row = updateOrder(lyhmsOrder);
        String roomName = lyhmsOrder.getRoomName();
        lyhmsRoomMapper.updateByName(roomName, 3);
        return row;
    }
}
